package com.bite.demo.controller;

import com.bite.demo.model.BlogInfo;
import com.bite.demo.model.PageResult;
import com.bite.demo.model.RequestPage;
import com.bite.demo.model.Result;
import com.bite.demo.service.BlogService;
import com.bite.demo.utils.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.ResultMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@Slf4j
@RequestMapping("/blog")
@RestController
public class BlogController {

    @Autowired
    private BlogService blogService;

    @RequestMapping("/getList")
    public List<BlogInfo> getList() {
       return blogService.getList();
    }

    @RequestMapping("/getListByPage")
    public Result<PageResult<BlogInfo>> getListByPage(RequestPage requestPage) {
        PageResult<BlogInfo> result =  blogService.getListByPage(requestPage);
        return  Result.success2(result);
    }



    @RequestMapping("/getBlogDetail")
    public BlogInfo getBlogDetail(Integer blogId,HttpServletRequest request) {
        log.info("getBlogDetail 接收参数 blogId:" +blogId);
        Assert.notNull(blogId,"Id不能为null");
        BlogInfo blogInfo = blogService.getBlogDetail(blogId);
        //获取作者Id
        Integer authorId = blogInfo.getUserId();

        //获取token
        String userToken = request.getHeader("user_token_header");
        //从token中获取用户Id
        Integer userId = JwtUtils.getUserIdFromToken(userToken);

        if(authorId!=null && authorId == userId) {
            blogInfo.setIsLoginUser(true);
        }
        return blogInfo;
    }
    /**
     * 发布博客
     */

    @RequestMapping("/addBlog")
    public boolean addBlog(@Validated @RequestBody BlogInfo blogInfo,HttpServletRequest request) {
        log.info("addBlog 接收参数blogInfo:" + blogInfo);
        //从header中获取token
        String userToken = request.getHeader("user_token_header");
        //从token获取userId
        Integer userId = JwtUtils.getUserIdFromToken(userToken);
        blogInfo.setUserId(userId);
        Integer result = null;
        try {
            result =  blogService.insertBlog(blogInfo);
            if(result == 1) {
                return  true;
            }

        }catch (Exception e) {
            log.error("发布博客失败 e"+e);
        }
        return false;
    }

    /**
     * 更新博客
     */
    @RequestMapping("/updateBlog")
    public Boolean updateBlog(@Validated @RequestBody BlogInfo blogInfo) {
        log.info("接收参数 blogInfo："+blogInfo);
        Integer result = null;
         try {
             result =blogService.updateBlog(blogInfo);
             if(result == 1) {
                 return  true;
             }

         }catch (Exception e) {
             log.error("updateBlog 发送异常 e:" +e);
         }
         return false;
    }
    /**
     * 删除博客
     */
    @RequestMapping("/deleteBlog")
    public Boolean deleteBlog(Integer blogId) {
        Assert.notNull(blogId,"blogId不能为空");
        log.info("接收参数 blogId："+blogId);
        Integer result = null;
        try {
            BlogInfo blogInfo = new BlogInfo();
            blogInfo.setId(blogId);
            blogInfo.setDeleteFlag(1);
            result =blogService.updateBlog(blogInfo );
            if(result == 1) {
                return  true;
            }
        }catch (Exception e) {
            log.error("deleteBlog 发送异常 e:" +e);
        }
        return false;
    }
}
